<template>
  <div>
    <el-form :model="form" inline>
      <el-form-item label="试卷标题">
        <el-input v-model="form.title" placeholder="请输入试卷标题"></el-input>
      </el-form-item>
      <el-form-item label="考试时间">
        <DatePicker
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          v-model="form.time"
          placeholder="请输入结束时间"
        ></DatePicker>
      </el-form-item>
      <el-button type="primary" @click="confirm">确 认</el-button>
      <el-button type="primary" @click="reset">重 置</el-button>
    </el-form>
    <el-table :data="tableData">
      <el-table-column label="试卷标题" prop="title"></el-table-column>
      <el-table-column label="姓名" prop="username"></el-table-column>
      <el-table-column label="得分" prop="score"></el-table-column>
      <el-table-column label="考试时间" prop="usedTime"></el-table-column>
      <el-table-column label="交卷时间" prop="createTime"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="getMyExam(scope.row)" size="small"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json 文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import testResultApi from "@/api/questionItem/testResult";
import { DatePicker } from "element-ui";
export default {
  name: "QuestionItem",
  //import 引入的组件需要注入到对象中才能使用
  components: { DatePicker },
  props: {},
  data() {
    //这里存放数据
    return {
      form: { title: "", time: "" },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    /**
     * 点击获取考卷的方法
     * @param {} data
     */
    getMyExam(data) {
      this.$router.push({
        path: "/testAnswer/index",
        query: {
          questionItemId: data.questionItemId, // 替换paramName和paramValue为实际的参数名和值
          account: data.account,
        },
      });
    },
    handleCurrentChange(currentPage) {
      this.list(currentPage);
    },
    list: async function (currentPage) {
      const res = await testResultApi.getList(
        this.form,
        currentPage,
        this.pageSize
      );
      this.tableData = res.data;
    },
    getTotal: async function () {
      const res = await testResultApi.getTotal(this.form);
      this.total = res.data;
    },
    confirm() {
      this.tableData = [];
      this.total = 0;
      this.getTotal();
      this.list(1);
    },
    reset() {
      this.form = { questionItemId: "", time: "" };
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    this.form.title = this.$route.query.title;
    this.list(1);
    this.getTotal();
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped></style>
